package com.weathereport.activity;

import com.weathereport.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;

public class SettingsActivity extends Activity implements OnClickListener{
	
	private Context ctx;
	private RadioGroup genderRg;
	private RadioButton genderRbSelected;
	private SeekBar	feelBr;
	private Button saveBt;
	private int genderSelected;
	private double fellBrSelected;
	private Intent nav;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.settings);
		
		ctx = this;
		getReferences();
	}
	
	public void getReferences(){
		
		genderRg	= (RadioGroup) findViewById(R.id.genderRG);			
		feelBr		= (SeekBar) findViewById(R.id.feelBar);		
		saveBt		= (Button) findViewById(R.id.saveBT);		
		saveBt.setOnClickListener(this);
		
		SharedPreferences settings = ctx.getSharedPreferences("prefs", Context.MODE_PRIVATE);
        
        String gender = settings.getString("gender", null);
        
        if (gender != null){
        	
        	if (gender.equals("Feminino")){
        		
        		genderRbSelected = (RadioButton) genderRg.findViewById(R.id.genderRG1);
        		genderRbSelected.setChecked(true);
        	}else {
        		
        		genderRbSelected = (RadioButton) genderRg.findViewById(R.id.genderRG2);
        		genderRbSelected.setChecked(true);
        	}
        	
	        int progress = settings.getInt("positionBar", 0);
			feelBr.setProgress(progress);
        }
                
	}

	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.saveBT:
			
			genderSelected = genderRg.getCheckedRadioButtonId();
			genderRbSelected = (RadioButton) genderRg.findViewById(genderSelected);
			
			fellBrSelected = convertFeel(feelBr.getProgress());
			
			SharedPreferences settings = getSharedPreferences("prefs", 0);
	        SharedPreferences.Editor editor = settings.edit();

	        editor.putString("gender", genderRbSelected.getText().toString());	        
	        editor.putFloat("feel", (float) fellBrSelected);
	        editor.putInt("positionBar", feelBr.getProgress());
	        editor.commit();
	        
	        Toast.makeText(ctx, R.string.msgSaveSuccess, Toast.LENGTH_SHORT).show();
	        goToMain(ctx);
	        
			break;
		default:
			break;
		}
	}

	public double convertFeel(double feel){
		return round((feel / -10 ) + 1, 2);
	}
	
	public double round(double valor, int casas) {  
		valor *= (Math.pow(10, casas));  
		valor = Math.round(valor);           
		valor /= (Math.pow(10, casas));  
	 
		return valor;  
	}
	
	public int reConvertFeel(int feel){
		
		return (feel - 1)  * -10;
	}
	
	public void goToMain(Context context){
		
		nav = new Intent(ctx, WeatheReportMain.class);
		nav.putExtra("firstRun", false);
		startActivity(nav);
	}
}
